<?php
/*
 * File: UserMember.php
 * Project: NewDemo
 * Created Time: 2024-06-21 02:45:57 pm
 * Author: WangYuQiang && WangpeiGang (719404171@qq.com)
 * -----
 * Last Modified: 2024-07-24 4:20:15 pm
 * Modified By: WangYuQiang (719404171@qq.com>)
 * -----
 * Copyright (c) 2023 - 2024 成都睿創科技有限公司
 */



namespace App\Models;

use DateTimeInterface;
use App\Models\Base;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Auth\User as Authenticatable;
/**
 * App\Models\UserMember
 *
 */
class UserMember extends base 
{
    use Notifiable;
    protected $table = 'user_member';
    protected $primaryKey = 'member_id';
    public $timestamps = true;

    /**
     * 为数组 / JSON 序列化准备日期。
     */
    protected function serializeDate(DateTimeInterface $date){
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }


    /**
     * 函數構造
     */
    public function __construct(){
        $this->language = app('global_language') == "en" ? "en_" : "";
        $this->AppDetail = ['member_id',$this->language.'member_name as member_name','member_start','member_end','member_status','created_at','updated_at'];
    }


/*********************************************************全局作用域注册**********************************************************************/
    protected static function boot(){
       parent::boot();

	   static::addGlobalScope('user_member', function (Builder $builder) {
		  return $builder->where('user_member.member_status', '<>', -1);
	   });
    }



/*********************************************************admin**********************************************************************/
    /**
     * 修改會員等級
     * @param $data
     * @return array
     */
    public function editMemberListWeb($data){
        DB::beginTransaction();
        $status = true;
        for($i=0;$i<count($data);$i++){
            if(empty($data[$i]['member_id'])){
                $data[$i]['created_at'] = self::getDate();
            }
            $datas = $this->updateOrInsert(['member_id'=>$data[$i]['member_id']], $data[$i]);
            if(!$datas){
                $status = false;
                break;
            }
        }

        if($status){
            DB::commit();
            return $this->success(__('web.set_success'));
        }else{
            DB::rollBack();
            return $this->error(__('web.set_fail'));
        }
    }

/**********************************************************api**********************************************************************/
    /**
     * @return mixed
     * 獲取會員等級信息
     */
    public function getUserMemberrApi($data){
        $limit = empty($data['limit']) ? 10 : $data['limit'];
        $page =  ( $data['page'] - 1 ) * $limit;
        $where[] = array('member_status','<>',-1);
        $effects=array();
        $info = $this ->where($where)->select($this->AppDetail)->orderBy('member_id','desc')->orderBy('created_at','desc')->paginate($limit , $page);
        $effects=$info->items();
        $results['count']=$info->count();
        $results['currentPage']=$info->currentPage();
        $results['limit']=$info->perPage();
        $results['lastPage']=$info->lastPage();
        $results['data']=$effects;
        if(!empty($effects)){
            return $this->success( __('api.list_success'), $results);
        }else{
            return $this->success( __('api.list_null'), $results);
        }
    }

/**************************************************common(api和admin共用)***********************************************************/

}
